#include <ft2build.h>
#include FT_FREETYPE_H 
#include <stdio.h>

#include <holt.h>

void
bitmap_to_bytemap (const FT_Bitmap *bitmap, unsigned char *bytemap,
                   unsigned int x, unsigned int y)
{
    unsigned int bx, by;
    unsigned char c, *row, *byte_base;
        
    for (by = 0, row = bitmap->buffer;
         by < (unsigned int) bitmap->rows; by++, row += bitmap->pitch) {

        //first convert full bytes at a time
        byte_base = &bytemap[(y+by) * HOLT_LCD_WIDTH + x];

        for (bx = 0; bx < ( (unsigned int) bitmap->width / 8) * 8; bx += 8) {
            c = row[bx/8];
            byte_base[bx+0] = (c & 0x80) ? 0xff : 0;
            byte_base[bx+1] = (c & 0x40) ? 0xff : 0;
            byte_base[bx+2] = (c & 0x20) ? 0xff : 0;
            byte_base[bx+3] = (c & 0x10) ? 0xff : 0;
            byte_base[bx+4] = (c & 0x08) ? 0xff : 0;
            byte_base[bx+5] = (c & 0x04) ? 0xff : 0;
            byte_base[bx+6] = (c & 0x02) ? 0xff : 0;
            byte_base[bx+7] = (c & 0x01) ? 0xff : 0;
        }
        
        //finally, convert the remaining bits
        c = row[bx/8];
        for (; bx < (unsigned int) bitmap->width; bx++) {
            byte_base[bx] = (c & 0x80) ? 0xff : 0;
            c <<= 1;
        }
    }
}    

int main (void) {
    FT_Library library;
    FT_Face face;
    FT_Error ferr;
    char text[] = "Hello World!";
    unsigned int u, x, y;
    unsigned char bytemap[HOLT_LCD_BYTEMAP_SIZE];
    unsigned char bitmap[HOLT_LCD_BITMAP_SIZE];
    struct input_event ev;
    
    ferr = FT_Init_FreeType(&library);
    if (ferr) {
        printf("Initialization error  %d\n", ferr);
        exit(1);
    }
    
    ferr = FT_New_Face(library, "/usr/share/fonts/misc/9x15.pcf.gz",
                       0, &face); 
    if (ferr == FT_Err_Unknown_File_Format) {
        printf("Unsupported font\n");
        exit(2);
    } else if (ferr) {
        printf("Face error %d\n", ferr);
        exit(3);
    }

    ferr = FT_Set_Pixel_Sizes(face, 9, 15);
    if (ferr) {
        printf("Size error %d\n", ferr);
        exit(4);
    }
    
    memset(bytemap, 0, sizeof(bytemap));
    
    for (u = 0; u < sizeof(text) - 1; u++) {
        ferr = FT_Load_Char(face, text[u],
                            FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
        if (ferr) continue; /* ignore errors */

        bitmap_to_bytemap(&face->glyph->bitmap, bytemap, u * 9, 42);
        
    //     unsigned char c, *row = face->glyph->bitmap.buffer, *col8;
    //     unsigned int height = face->glyph->bitmap.rows;
    //     unsigned int width = face->glyph->bitmap.width;
    //     unsigned int pitch = face->glyph->bitmap.pitch;
        
    //     printf("%c\n", text[u]);

    //     for (y = 0; y < height; y++, row += pitch) {
    //         //first print all full bytes of each line
    //         for (col8 = row; col8 < row + width / 8; col8++) {
    //             c = *col8;
    //             for (x = 0; x < 8; x++) {
    //                 printf("%c", (c & 0x80) ? 'X' : '-');
    //                 c <<= 1;
    //             }
    //         }
            
    //         //finnaly, print the remaining bits
    //         c = *col8;
    //         for (x = (col8 - row) * 8; x < width; x++) {
    //             printf("%c", (c & 0x80) ? 'X' : '-');
    //             c <<= 1;
    //         }
    //         printf("\n");
    //     }

    }

    holt_init(NULL);
    
    holt_lcd_convert_bytemap(bytemap, bitmap);
    holt_lcd_bitmap(bitmap);

    do {
        holt_read_event(&ev);
    } while ( !(ev.type == EV_KEY && ev.code == KEY_PHONE && ev.value == 0) );
    
    holt_deinit();
    
    return 0;
}

